package com.eonmind.ai.service.impl;

import com.eonmind.ai.mapper.AiDrawPlatformMapper;
import com.eonmind.ai.service.IAiDrawPlatformService;
import com.eonmind.common.contract.enums.StatusEnum;
import com.eonmind.common.contract.exception.BusinessException;
import com.eonmind.common.provider.entity.ai.AiDrawPlatform;
import com.mongoplus.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Service
public class AiDrawPlatformService extends ServiceImpl<AiDrawPlatform> implements IAiDrawPlatformService {

    @Resource
    private AiDrawPlatformMapper aiDrawPlatformMapper;

    @Override
    public AiDrawPlatform randomGetOnePlatform(Integer code) {
        List<AiDrawPlatform> aiDrawPlatformList =  aiDrawPlatformMapper.selectPlatformByStatus(StatusEnum.OPENED.getCode());
        if(ObjectUtils.isEmpty(aiDrawPlatformList)) {
            throw new BusinessException("未找到可用的绘图平台");
        }
        // 随机数
        int random = (int)(Math.random() * aiDrawPlatformList.size());

        return aiDrawPlatformList.get(random);
    }

    @Override
    public AiDrawPlatform selectById(Long platformId) {

        return aiDrawPlatformMapper.selectPlatformById(platformId);
    }
}
